package com.cekanet.mybudget;

import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.*;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.cekanet.mybudget.dbutils.StatementHandler;
import com.cekanet.mybudget.dbutils.StatementItem;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class GetStatementServlet extends  HttpServlet{
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try{
			DocumentBuilderFactory docFactory= DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("response");
			doc.appendChild(rootElement);
			
			
			Element exitStatus = doc.createElement("exitStatus");
			rootElement.appendChild(exitStatus);
			
			exitStatus.setAttribute("status", "OK");
			Element statDescription = doc.createElement("description");
			exitStatus.appendChild(statDescription);
			statDescription.appendChild(doc.createTextNode("Job done !"));
			
			Element statementEl = doc.createElement("statement");
			rootElement.appendChild(statementEl);
			statementEl.setAttribute("type","full");
			
			User user = UserServiceFactory.getUserService().getCurrentUser();
			if (user == null)
				throw new UserException("User service returned null.");
			
			StatementHandler statementHandler = new StatementHandler(user.getUserId());
			List<StatementItem> statement = statementHandler.getUserStatement();
				
			if (!statement.isEmpty()){
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
				DecimalFormat currencyFormat = new DecimalFormat();
				currencyFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ITALY));
				currencyFormat.setGroupingUsed(false);
				
				for (StatementItem si : statement){
					Element item = doc.createElement("item");
					statementEl.appendChild(item);
					item.setAttribute("id", si.getId().toString());
	
					Element date = doc.createElement("date");
					date.appendChild(doc.createTextNode(dateFormat.format(si.getDate())));
					item.appendChild(date);
	
										
					Element price = doc.createElement("price");
					price.appendChild(doc.createTextNode(currencyFormat.format(si.getPrice())));
					item.appendChild(price);
	
					Element name = doc.createElement("name");
					name.appendChild(doc.createTextNode(si.getName()));
					item.appendChild(name);
	
					Element description = doc.createElement("description");
					description.appendChild(doc.createTextNode(si.getDescription()));
					item.appendChild(description);
	
				}
			}

			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);


			resp.setContentType("text/xml");
			StreamResult result = new StreamResult(resp.getWriter());
			transformer.transform(source, result);
			

		}catch (UserException e){
			System.err.println(e.getMessage());
			e.printStackTrace();
			
		}catch (ParserConfigurationException e) {
			e.printStackTrace();
		}catch (TransformerConfigurationException e) {
			e.printStackTrace();
		}catch (TransformerException e) {
			e.printStackTrace();
		}

	}
}
